//index.js
//获取应用实例
const app = getApp()
const util = require("../../utils/util.js");
var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
Page({
    data: {
        menuIndex: 0,
        menuName: '政务办事',
        showOld: false,
        showNew: false,
        // cityname: app.globalData.extConfig.cityname,
        // citycode: app.globalData.extConfig.citycode,
        showtk: false,
        path: '',
        city: "深圳",
        citycode: 'sz',
        item: {
            showtk: false,
            path: ''
        },
        bsylistData: [],
        hotztData: [], // 热门办事
        bsPeopleData: [], //办事人群
        // allData:["政务办事","教育办事","交通出行","车辆服务","民生服务"],
        allData: [{
            name: "政务办事",
            info: []
        }, {
            name: "教育办事",
            info: []
        }, {
            name: "交通出行",
            info: []
        }, {
            name: "车辆服务",
            info: []
        }, {
            name: "民生服务",
            info: []
        }, ],

        showloading: true,
    },

    onLoad: function (opt) {
        this.setData({
            citycode: opt.city ? opt.city : (ks.getStorageSync("citycode") ? ks.getStorageSync("citycode") : this.data.citycode),
        })
        ks.setStorageSync('citycode', this.data.citycode)

        util.getUrl(app.globalData.url).then((res) => {
            util.cityList().then((res) => {
                let allcity = res.allcity;
                if (!ks.getStorageSync("city") && !opt.city) {
                    qqmapsdk.reverseGeocoder({
                        success: (res) => {
                            let city = res.result.address_component.city;
                            let citydata = allcity.filter(v => {
                                if (city != undefined) {
                                    if (city.indexOf(v.cityname) != -1) {
                                        return v;
                                    }
                                }
                            });
                            if (city.indexOf(ks.getStorageSync("city")) == -1) {
                                if (citydata[0]) {
                                    let citycode = citydata[0] ? citydata[0].citycode : '';
                                    ks.showModal({
                                        title: '温馨提醒',
                                        content: '当前城市与您定位城市不一样，是否切换城市？',
                                        success: res => {
                                            if (res.confirm) {
                                                ks.reLaunch({
                                                    url: '/pages/bsy/bsy?city=' + citycode,
                                                })
                                            }
                                        }
                                    })
                                } else {
                                    return;
                                }
                            }
                        },
                    });
                }
                let citydatas;
                if (Array.isArray(allcity) && allcity)
                    citydatas = allcity.filter(v => v.citycode == this.data.citycode);
                this.setData({
                    city: citydatas[0] ? citydatas[0].cityname : this.data.city,
                })
                ks.setStorageSync('city', this.data.city)
                this.setData({
                    showloading: true,
                })
                this.getBannerData();
                this.getbsylist();
                this.getHotList();
            })
            // this.getCityConfig();
        });
    },

    // 首页swiper-banner
    getBannerData() {
        let bannerData = {
            action: 'lunbo',
            city: ks.getStorageSync("city"),
            block: "办事攻略",
            blockvalue: "",
            version: app.globalData.version,
        };
        util.request('service.php', bannerData, ).then(res => {
            this.setData({
                bannerArray: res.data.data,
            })
        })
    },

    // 办事频道
    getbsylist() {
        let data = {
            citycode: this.data.citycode == "sz" ? "bsy" : this.data.citycode,
            action: "sitelist",
            pagesize: 4,
            bsy: 1,
            version: app.globalData.version,
        };
        util.request('index.php', data, 'GET').then(res => {
            this.setData({
                bsylistData: res.data,
            })
            this.setData({
                showloading: false,
            })
        })
    },

    // 热点
    getHotList() {
        let hotData = {
            action: "index",
            city: ks.getStorageSync("city"),
            citycode: ks.getStorageSync("citycode"),
            version: app.globalData.version,
        };
        util.request('service.php', hotData, ).then(res => {
            if (res.data.data)
                this.setData({
                    hotList: res.data.data.hot
                });
            this.setData({
                showloading: false,
            });
        });
    },

    // banner点击事件
    bannerClick(e) {
        let path = e.currentTarget.dataset.path;
        let linkname = e.currentTarget.dataset.title;
        let appid = e.currentTarget.dataset.appid;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            this.setData({
                showtk: true
            })
            util.navContact(linkname, path)
        } else {
            util.navMethod(path, appid);
        }

    },

    // 栏目点击事件
    lanmuTap(e) {
        let id = e.currentTarget.dataset.id;
        let type = e.currentTarget.dataset.type;
        // let name = e.currentTarget.dataset.name;
        ks.navigateTo({
            url: '../article/list/list?id=' + id + '&type=' + type + '&city=' + this.data.citycode,
        })
    },

    // 工具、服务点击事件
    servBtn(e) {
        let path = e.currentTarget.dataset.path;
        let appid = e.currentTarget.dataset.appid;
        let linkname = e.currentTarget.dataset.title;
        this.data.item.path = path;

        this.setData({
            path,
            linkname,
            // item,
        })
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            util.navContact(linkname, path)
            this.setData({
                showtk: true
            })
        } else {
            util.navMethod(path, appid);
        }
    },

    // 热门专题、全部办事事件
    bsyClickBtn(e) {
        let path = e.currentTarget.dataset.path;
        let appid = e.currentTarget.dataset.appid;
        let title = e.currentTarget.dataset.title;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            util.navContact(title, path)
            this.setData({
                showtk: true
            })
        } else {
            util.navMethod(path, appid);
        }
    },

    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
        let replay = {
            action: "reply",
            title: this.data.linkname,
            url: this.data.path,
            openid: ks.getStorageSync("openid")
        }
        util.request('user.php', replay, 'POST')
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },

    // 全部
    clickMenu: function (e) {
        let menuIndex = e.currentTarget.dataset.index;
        let menuName = e.currentTarget.dataset.name;
        this.setData({
            menuIndex,
            menuName,
        })
        this.getallBsy(menuName, menuIndex);

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: "本地宝-办事攻略",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },

    onShareTimeline: function () {
        return {
            title: "本地宝",
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },



})